package com.alen.sun.controller;

import com.alen.sun.bean.Folk;
import com.alen.sun.bean.Order;
import com.alen.sun.bean.PageBean;
import com.alen.sun.bean.Result;
import com.alen.sun.service.ActivityService;
import com.alen.sun.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/orders")
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * @param order
     * @return
     */
    @PostMapping
    @ResponseBody
    public Result addOrder(@RequestBody Order order){
        Boolean flag = orderService.addOrder(order);
        if(flag){
            return Result.success();
        }else {
            return Result.error("订单添加失败");
        }
    }

    /**
     * 修改订单
     * @param order
     * @return
     */
    @PutMapping
    @ResponseBody
    public Result updateOrder(@RequestBody Order order){
        Boolean flag = orderService.updateOrder(order);
        if(flag){
            return Result.success();
        }else {
            return Result.error("订单修改失败");
        }
    }

    /**
     * 删除订单
     * @param order
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public Result deleteOrder(@RequestBody Order order){
        Boolean flag = orderService.deleteOrder(order);
        if(flag){
            return Result.success();
        }else {
            return Result.error("订单删除失败");
        }
    }

    /**
     * 分类查询
     * @param requestParams
     * @return
     */
    @PostMapping("/selectByPage")
    @ResponseBody
    public Result page(@RequestBody Map<String, Integer> requestParams) {
        Integer page = requestParams.getOrDefault("page", 1);
        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
        PageBean pageBean = orderService.page(page, pageSize);
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = orderService.deleteByIds(ids);
        if(flag){
            return Result.success();
        }else {
            return Result.error("批量删除失败");
        }

    }

    /**
     * 非基础功能
     */

    //家属端,获取所有订单信息
    @PostMapping("getOrderByElderlyIdAll")
    @ResponseBody
    public Result getOrderByElderlyIdAll(@RequestBody Map<String, Object> requestBodyMap){
        String elderlyId = (String) requestBodyMap.get("elderlyId");
        Integer page = (Integer) requestBodyMap.get("page");
        Integer pageSize = (Integer) requestBodyMap.get("pageSize");
        PageBean orders = orderService.getOrderByElderlyIdAll(elderlyId,page,pageSize);
        return Result.success(orders);
    }

    //家属端,获取未支付订单信息
    @PostMapping("getOrderByElderlyIdUnpaid")
    @ResponseBody
    public Result getOrderByElderlyIdUnpaid(@RequestBody Map<String, Object> requestBodyMap){
        String elderlyId = (String) requestBodyMap.get("elderlyId");
        Integer page = (Integer) requestBodyMap.get("page");
        Integer pageSize = (Integer) requestBodyMap.get("pageSize");
        PageBean orders = orderService.getOrderByElderlyIdUnpaid(elderlyId,page,pageSize);
        return Result.success(orders);
    }

    //家属端,获取已支付订单信息
    @PostMapping("getOrderByElderlyIdPaid")
    @ResponseBody
    public Result getOrderByElderlyIdPaid(@RequestBody Map<String, Object> requestBodyMap){
        String elderlyId = (String) requestBodyMap.get("elderlyId");
        Integer page = (Integer) requestBodyMap.get("page");
        Integer pageSize = (Integer) requestBodyMap.get("pageSize");
        PageBean orders = orderService.getOrderByElderlyIdPaid(elderlyId,page,pageSize);
        return Result.success(orders);
    }

    /**
     * 网页搜索功能 聚合模糊查询, 订单编号,套餐名
     * @param JsonData
     * @return
     */
    @PostMapping("/searchByKeywords")
    @ResponseBody
    public Result searchByKeywords(@RequestBody Map<String,String> JsonData){
        String keywords = JsonData.get("keywords");
        List<Order> orders = orderService.searchByKeywords(keywords);
        if(!orders.isEmpty()){
            return Result.success(orders);
        }else {
            return Result.error("未查询到相关结果");
        }
    }

    /**
     * 数据可视化分析,日期,当日套餐总收入
     * @return
     */
    @GetMapping("/getOrderTotalAmountAndTime")
    @ResponseBody
    public Result getOrderTotalAmountAndTime(){
        List<Map<String, Object>> resultList = orderService.getOrderTotalAmountAndTime();
        return Result.success(resultList);
    }

    /**
     * 套餐截止日期提醒
     * @return
     */
    @GetMapping("/getDaysUntil")
    @ResponseBody
    public Result getDaysUntil(){
        List<Map<String, Object>> daysUntil = orderService.getDaysUntil();
        return Result.success(daysUntil);
    }


}
